let net = require('net')

let server = net.createServer()
let port = 8088
let sockets = []

server.on('connection', socket => {
    console.log('Got a new connection')

    sockets.push(socket)

    socket.on('data', data => {
        console.log('Got data: ', data.toString())

        sockets.forEach(otherSocket => {
            if (otherSocket !== socket) {
                otherSocket.write(data)
            }
        });
    })

    socket.on('close', () => {
        console.log('A client connection closed');
        let index = sockets.indexOf(socket)
        sockets.splice(index, 1)
    })
})
server.on('error', err => {
    console.log('Server error', err.message)
})

server.on('close', () => {
    console.log('Server closed')
})

server.listen(port)